using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using DotNetNuke.Framework.Providers;

namespace gdlmo.DNN.Modules.Informations.Data 
{
    
	public class SqlDataProvider : DataProvider 
	{
        
		private const string providerType = "data";
        
		#region Private Members
		private ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(providerType);
		private string _connectionString;
		private string _providerPath;
		private string _objectQualifier;
		private string _databaseOwner;
		#endregion
        
		#region Constructors
		public SqlDataProvider()
		{
			Provider objProvider = ((Provider)_providerConfiguration.Providers[_providerConfiguration.DefaultProvider]);
			if (objProvider.Attributes["connectionStringName"] != "" && System.Configuration.ConfigurationSettings.AppSettings[objProvider.Attributes["connectionStringName"]] != "") 
			{
				_connectionString = System.Configuration.ConfigurationSettings.AppSettings[objProvider.Attributes["connectionStringName"]];
			} 
			else 
			{
				_connectionString = objProvider.Attributes["connectionString"];
			}
			_providerPath = objProvider.Attributes["providerPath"];
			_objectQualifier = objProvider.Attributes["objectQualifier"];
			if (_objectQualifier != "" & _objectQualifier.EndsWith("_") == false) 
			{
				_objectQualifier += "_";
			}
			_databaseOwner = objProvider.Attributes["databaseOwner"];
			if (_databaseOwner != "" & _databaseOwner.EndsWith(".") == false) 
			{
				_databaseOwner += ".";
			}
		}
		#endregion

		#region Properties
		public string ConnectionString 
		{
			get 
			{
				return _connectionString;
			}
		}

		public string ProviderPath 
		{
			get 
			{
				return _providerPath;
			}
		}

		public string ObjectQualifier 
		{
			get 
			{
				return _objectQualifier;
			}
		}

		public string DatabaseOwner 
		{
			get 
			{
				return _databaseOwner;
			}
		}
		#endregion

		#region Private Methods
		private object GetNull(object Field)
		{
			return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value);
		}

		#endregion
		
		#region "Informations Methods"
		public override IDataReader GetInformations(int InfoId, int moduleId)
		{
			return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "gdlmo_InformationsGet", InfoId,moduleId);
		}
		
		public override IDataReader ListInformations()
		{
			return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "gdlmo_InformationsList");
		}
		
		public override int AddInformations(int ModuleId , string Title , string URL , string Description , int ViewOrder , DateTime CreateDate , DateTime ExpireDate , string Publisher , string DeptName , DateTime PublishDate , string CreatedByUser)
		{
			return int.Parse(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "gdlmo_InformationsAdd", ModuleId, Title, URL, Description, ViewOrder, CreateDate, ExpireDate, Publisher, DeptName, PublishDate , CreatedByUser.ToString()).ToString());
		}
			
		public override void UpdateInformations(int InfoId, int ModuleId , string Title , string URL , string Description , int ViewOrder , DateTime CreateDate , DateTime ExpireDate , string Publisher , string DeptName , DateTime PublishDate , string CreatedByUser)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "gdlmo_InformationsUpdate", InfoId, ModuleId, Title, URL, Description, ViewOrder, CreateDate, ExpireDate, Publisher, DeptName, PublishDate , CreatedByUser);
		}
		
		public override void DeleteInformations(int InfoId)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "gdlmo_InformationsDelete", InfoId);
		}

		public override IDataReader GetByModule(int moduleId)
		{
			return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "gdlmo_InformationsGetByModule",moduleId);
		}
		
		public override IDataReader GetPageData(int PageIndex,int PageSize)
		{
			string strWhere =STRWHERE;
//			int Rcount = (int)(SqlHelper.ExecuteScalar(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "gdlmo_GetCount",TABLENAME,KEYFIELD,strWhere));
//			if ((Rcount % PageSize) > 0)
//			{
//				PageCount = (Rcount / PageSize) + 1;
//			}
//			else
//			{
//				PageCount = Rcount / PageSize;
//			}	
			return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "gdlmo_GetPagingData", TABLENAME,FIELDLIST,ORDERFIELD,KEYFIELD,PageIndex,PageSize,strWhere,ORDERTYPE);		
		}
		public override IDataReader GetById(int InfoId)
		{
			return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "gdlmo_InformationsGetById", InfoId);
		}
		#endregion
	}
}